local battleSceneLoader = require "Battle/BattleSceneLoader"
FlyWordBar = { }
local this = FlyWordBar

function FlyWordBar.init()
    this.fontHPDec = battleSceneLoader.loadNoClone("HPDec")
    this.fontHPInc = battleSceneLoader.loadNoClone("HPInc")
    this.gameObject = battleSceneLoader.loadUI("FlyWordBar")
    this.temFlyWord = UnityUtil.GetChildBone(this.gameObject.transform, "temFlyWord")
    this.temFlyWord.gameObject:SetActive(false)
end

function FlyWordBar:showHPDec(...)
    local uiText = this.createText();
    uiText.font = this.fontHPDec
    FlyWord.new(uiText, ...)
end

function FlyWordBar:showHPInc(...)
    local uiText = this.createText();
    uiText.font = this.fontHPInc
    FlyWord.new(uiText, ...)
end

function FlyWordBar:createText()
    local gameObject = UnityEngine.GameObject.Instantiate(this.temFlyWord)
    gameObject.gameObject:SetActive(true)
    local transform = gameObject:GetComponent(typeof(UnityEngine.Transform))
    transform:SetParent(this.temFlyWord.parent)
    transform.localPosition = Vector3.New(0, 0, 0)
    return gameObject:GetComponent(typeof(UnityEngine.UI.Text))
end

function FlyWordBar.clear()
    UnityUtil.Destroy(this.gameObject)
    this.gameObject = nil
    this.fontHPDec = nil
    this.fontHPInc = nil
    this.temFlyWord = nil
end

FlyWord = class("FlyWord")

function FlyWord:ctor(...)
    local arg = { ...}
    --    GameLog.Error(arg[1])
    --    GameLog.Error(arg[2])
    --    GameLog.Error(arg[3])
    self.uiText = arg[1]
    self.unitData = arg[2]
    self.color = self.uiText.color
    self.rectTransform = self.uiText:GetComponent(typeof(UnityEngine.RectTransform))
    if (arg[3] < 0) then
        self.uiText.text = arg[3]
    else
        self.uiText.text ="+" .. arg[3]
    end
    --    GameLog.Error(self.unitData)
    self:freshInitPosition()
    self.updatePosition = Vector3.New()
    self:reset()
    UpdateBeat:Add(self.update, self);
end

function FlyWord:freshInitPosition()
    --    GameLog.Error(BattleConfigData.flyWord.hitBoneName)
    self.initPosition = UnityEngine.Camera.main:WorldToViewportPoint(self.unitData.flyWordTransform.position)
    -- GameLog.Error(self.initPosition)
    -- GameLog.Error(self.uiText.canvas.pixelRect)
    self.initPosition.x =(self.initPosition.x - 0.5) * self.uiText.canvas.pixelRect.width;
    self.initPosition.y =(self.initPosition.y - 0.5) * self.uiText.canvas.pixelRect.height;
    -- GameLog.Error(self.initPosition)
end

function FlyWord:reset()
    self.passTime = 0
    self.color.a = 1
    self.uiText.color = self.color
end

function FlyWord:update()
    self.passTime = self.passTime + Time.deltaTime
    if (self.passTime >= BattleConfigData.flyWord.lifeTime) then
        UnityUtil.Destroy(self.uiText.gameObject)
        UpdateBeat:Remove(self.update, self)
        return
    end
    if (self.passTime > BattleConfigData.flyWord.delayTime) then
        local alpha =(BattleConfigData.flyWord.lifeTime - self.passTime) /(BattleConfigData.flyWord.lifeTime - BattleConfigData.flyWord.delayTime)
        -- GameLog.Error(alpha)
        self.color.a = alpha
        self.uiText.color = self.color
        -- GameLog.Error(BattleConfigData.flyWord.delayTime, self.passTime, BattleConfigData.flyWord.lifeTime)
        -- GameLog.Error(self.color.a)
        -- GameLog.Error(self.uiText.color.a)
        -- GameLog.Error(self.uiText.color)
    end
    self:freshInitPosition()
    if (self.unitData.isFriend) then
        self.updatePosition.x = self.initPosition.x - BattleConfigData.flyWord.speedx * self.passTime
    else
        self.updatePosition.x = self.initPosition.x + BattleConfigData.flyWord.speedx * self.passTime
    end
    self.updatePosition.y = self.initPosition.y + BattleConfigData.flyWord.speedy * self.passTime
    self.rectTransform.anchoredPosition3D = self.updatePosition

end